package com.example.demo.controller;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.ResultModel;
import com.example.demo.common.ResultModelTool;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.example.demo.common.JwtUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@Controller

public class loginController {
    @Resource
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;
    /**
     * 用户登录
     *  1.通过service根据mobile查询用户
     *  2.比较password
     *  3.生成jwt信息
     *  返回token
     *  例子
     *  {
     *     "code": 200,
     *     "date": "2020-03-22 21:08:26",
     *     "data": {
     *         "token": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyMDE5Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE1ODQ4ODI1MDYsInJvbGUiOjksImFjdGl2ZSI6MSwiZXhwIjoxNTg0OTE4NTA2fQ.HTbjzF5Ahr_jwtjN7gNmpvQHL5054bij3zOS5Y8XlFk"
     *     }
     * }
     */
    @RequestMapping(value="/web/login",method = RequestMethod.POST)
    @ResponseBody
    public ResultModel login(@RequestBody Map<String,String> loginMap) {
        String identity_id = loginMap.get("identity_id");
        String pwd = loginMap.get("pwd");
        User user = userService.queryById(identity_id);
        //登录失败
        if(user == null || !user.getPwd().equals(pwd)) {
            return new ResultModel(200,"用户名或密码错误");
        }else {
            //登录成功
            Map<String,Object> map = new HashMap<>();
            map.put("role",user.getRole());
            map.put("active",user.getActive());
            String token = jwtUtils.createJwt(user.getIdentity_id(),user.getName(), map);
            ResultModel resultModel = new ResultModel();
            Map<String,Object> usermap = new HashMap<>();
            usermap.put("token",token);
            usermap.put("name",user.getName());
            usermap.put("role",user.getRole());
            usermap.put("identity_id",user.getIdentity_id());
            resultModel.setCode(200);
            resultModel.setData(usermap);
            return ResultModelTool.handleResultModel(resultModel);
        }
    }
}
/**
 *  @PostMapping("/web/login")
 *     @ResponseBody
 *     public ResponseEntity login(@RequestParam String identityID,
 *                                 @RequestParam String password, HttpSession httpSession) throws UnsupportedEncodingException, JsonProcessingException {
 *         ResponseEntity responseEntity = ResponseEntity.ok();
 *         User userFromBase = userService.selectUserByIdentityId(identityID);
 *         if(userFromBase.getIdentityID().equals(identityID) &&
 *                 userFromBase.getPassword().equals(password)) {
 *             //给用户jwt加密生成token
 *             String token = JwtToken.sign(userFromBase, 60L * 1000L * 30L);
 *             //封装成对象返回给客户端
 *             responseEntity.putDataValue("userId", userFromBase.getIdentityID());
 *             responseEntity.putDataValue("token", token);
 *             httpSession.setAttribute("username",userFromBase.getUsername());
 *         }
 *         else{
 *             responseEntity =  ResponseEntity.customerError();
 *             responseEntity.putDataValue("msg","账号密码错误");
 *         }
 *         httpSession.setAttribute("username",userFromBase.getUsername());
 *         return responseEntity;
 *     }
 */
